package com.school.test.controller;

import com.school.common.core.web.controller.BaseController;
import com.school.common.core.web.domain.AjaxResult;
import com.school.common.core.web.page.TableDataInfo;
import com.school.common.log.annotation.Log;
import com.school.common.log.enums.BusinessType;
import com.school.common.security.annotation.RequiresPermissions;
import com.school.common.security.utils.SecurityUtils;
import com.school.test.domain.StatisticsVisit;
import com.school.test.domain.TestTable;
import com.school.test.domain.vo.ParamsVo;
import com.school.test.service.StatisticsVisitService;
import io.netty.util.internal.ObjectUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/StatisticsVisit")
public class StatisticsVisitController  extends BaseController {

    @Autowired
    private StatisticsVisitService statisticsVisitService;

    @PostMapping("/list")
    @ApiOperation(value="统计查询",notes = "查询内容为统计信息")
    public TableDataInfo list(@RequestBody ParamsVo paramsVo)
    {
        startPage();
        List<StatisticsVisit> list = statisticsVisitService.selectInfo(paramsVo);
        return getDataTable(list);
    }


    @PostMapping("/listInfo")
    @ApiOperation(value="普通查询查询",notes = "查询内容为所有信息")
    public TableDataInfo listInfo(@RequestBody ParamsVo paramsVo)
    {
        startPage();
        List<StatisticsVisit> list = statisticsVisitService.selectInfoAll(paramsVo);
        return getDataTable(list);
    }


    /**
     * 统计新增
     */
    @Log(title = "统计新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value="统计新增",notes = "新增统计，一个公司当天只能添加一次")
    public AjaxResult add(@Validated @RequestBody StatisticsVisit statisticsVisit)
    {
        //限制一天只能提交一次当天的记录
       List<StatisticsVisit> statisticsVisitList =  statisticsVisitService.selectInfoByCompanyType(null,statisticsVisit.getCompany_type(),statisticsVisit.getCreate_time(),statisticsVisit.getChannel());
       if(statisticsVisitList.size() > 0){
           return error("添加统计信息'" + "'失败，当天已经添加过统计信息，请确认");
       }
        return toAjax(statisticsVisitService.addStatisticsVisit(statisticsVisit));
    }


    /**
     * 统计修改
     */
    @Log(title = "统计修改", businessType = BusinessType.INSERT)
    @PostMapping("/update")
    @ApiOperation(value="统计修改",notes = "统计修改")
    public AjaxResult update(@Validated @RequestBody StatisticsVisit statisticsVisit)
    {
        //通过ID 查询 统计信息。
        StatisticsVisit visit = statisticsVisitService.selectInfoById(statisticsVisit.getId());
        if(null == visit){
            return error("通过ID查询数据为空，请确认");
        }
        String outputFormat = "yyyy-MM-dd";
                SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = inputDateFormat.parse(visit.getCreate_time());
            SimpleDateFormat outputDateFormat = new SimpleDateFormat(outputFormat);
            String outputDateStr = outputDateFormat.format(date);
            System.out.println("Formatted date: " + outputDateStr);
            visit.setCreate_time(outputDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //限制一天只能提交一次当天的记录
        List<StatisticsVisit> statisticsVisitList =  statisticsVisitService.selectInfoByCompanyType(statisticsVisit.getId(),statisticsVisit.getCompany_type(),statisticsVisit.getCreate_time(),statisticsVisit.getChannel());
        if(statisticsVisitList.size() > 0){
            return error("修改统计信息'" + "'失败，当天已经存在统计信息，请确认");
        }
        return toAjax(statisticsVisitService.updateStatisticsVisit(statisticsVisit));
    }




    @Log(title = "统计删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value="统计删除",notes = "删除统计数据，物理删除。")
    public AjaxResult remove(@PathVariable String id)
    {
        statisticsVisitService.deleteStatisticsVisitByIds(id);
        return success();
    }


}
